﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using System.Data;
using DreamBuilder.Core.Container;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellAttachDownload : PageBase
    {
        private string m_AttachmentID
        {
            get
            {
                return Request.QueryString["AttachmentID"];
            }
        }

        private string m_DBHelperString
        {
            get
            {
                return Request.QueryString["DBHelperString"];
            }
        }

        [Dependency]
        public IAttachmentService m_IAttachmentService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string attachmentName = string.Empty;
                string docTypeName = string.Empty;
                AttachmentModel model = this.GetAttachment();
                byte[] data = model.AttachmentContent;
                if (model != null && data != null)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(model.AttachmentName));
                    Response.AppendHeader("Content-Length", data.Length.ToString());
                    Response.ContentType = "APPLICATION/OCTET-STREAM";
                    Response.BinaryWrite(data);
                    Response.End();
                }
            }
        }

        #region 私有方法
        private AttachmentModel GetAttachment()
        {
            IAttachmentService oIAttachmentService = UContainer.Resolve<IAttachmentService>();
            oIAttachmentService.Initialize(this.m_DBHelperString);
            return oIAttachmentService.GetAttachmentModelByAttachmentID(this.m_AttachmentID);
        }
        #endregion
    }
}